<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="../common/taglibs.jsp"%>
<%@ page import="java.util.*"%>
<%@ page import="com.opensymphony.xwork2.ActionSupport"%>
<%@ page import="net.aiform.account.pojo.*"%>
<%@ page import="net.aiform.bean.Constant"%>
<%@ page import="net.aiform.template.pojo.*"%>
<%@ page import="ext.cjt.*"%>
<html>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<script src="${ctx}/static/js/jquery.js" type="text/javascript"></script>
<script src="${ctx}/static/js/jquery.url.js" type="text/javascript"></script>
<head>
<title>
<%
	ActionSupport as_index = new ActionSupport();
	out.print(as_index.getText("sys.title", new String[]{as_index
			.getText("sys.name")}));
%>
</title>
</head>
<body onload="init_template();">
<%@ include file="../common/userInfo.jsp"%>
<hr>
<form action="${ctx}/r/new/save" method="post">
<div id="recordarea"></div>
<button type="submit">Submit</button>
</form>
<script type="text/javascript">
	var _af_var_param = "params";
	var _af_var_RowIdentifier = "_af_row_id_";
	function init_template() {
		var templateName = jQuery.url.attr("file");
		fetch_template(templateName);
	}
	function customize_template(message) {
		if ("403" == message.code) {
			$("#recordarea").html("<h3 style='color:red'>没有权限</h3>");
			return;
		} else {
			var templateRecords = message.data.value;
			if (null == templateRecords)
				return;
			init_template_exe(templateRecords);
		}
	}
	function init_template_exe(templateRecords) {
		var template = templateRecords.template;
		var records = templateRecords.records;
		var fields = template.fields;
		$("#recordarea").append("<div>" + template.name + "</div>");
		$("#recordarea").append(
				"<input type='hidden' name='code' value='" + template.code
						+ "'></input>");
		init_template_fields(fields);
		init_records_value(records);
	}
	function init_records_value(records) {
		for ( var i = 0; i < records.length; i++) {
			var values = records[i].values;
			var code = records[i].code;
			$("#recordarea").append(
					"<input type='hidden' value='" + code + "' name='"
							+ _af_var_param + "[" + i + "]."
							+ _af_var_RowIdentifier + "' id='" + _af_var_param
							+ "[" + i + "]." + _af_var_RowIdentifier
							+ "'></input>");
			for ( var j = 0; j < values.length; j++) {
				var value = values[j];
				var key = value.code;
				var v = _af_getFieldValue(value);
				$("#" + _af_var_param + "[" + i + "]." + key).val(v);
			}
		}
	}
	function _af_getFieldValue(value) {
		if ("string" == value.dataType)
			return (value.s);
		if ("int" == value.dataType)
			return (value.i);
		if ("date" == value.dataType)
			return (value.d);
		if ("float" == value.dataType)
			return (value.f);
	}
	function init_template_fields(fields) {
		//按照field的position指定的位置来初始化
		for ( var p = 1; p <= fields.length; p++) {
			for ( var i = 0; i < fields.length; i++) {
				if ("" + p == fields[i].position)
					init_template_field(0, fields[i]);
			}
		}
	}
	function init_template_field(row, field) {
		if ("string" == field.category)
			init_field_string(row, field);
		if ("int" == field.category)
			init_field_int(row, field);
		if ("double" == field.category)
			init_field_double(row, field);
		if ("date" == field.category)
			init_field_string(row, field);
	}
	function init_field_string(row, field) {
		$("#recordarea").append("<div>");
		$("#recordarea").append("<span>" + field.name + "</span>");
		$("#recordarea").append(
				"<span>" + "<input name='" + _af_var_param + "[" + row + "]."
						+ field.code + "' id='" + _af_var_param + "[" + row
						+ "]." + field.code + "'/>" + "</span>");
		$("#recordarea").append("<span>" + field.remark + "</span>");
		$("#recordarea").append("</div>");
	}
	function init_field_int(row, field) {
		$("#recordarea").append("<div>");
		$("#recordarea").append("<span>" + field.name + "</span>");
		$("#recordarea").append(
				"<span>" + "<input name='" + _af_var_param + "[" + row + "]."
						+ field.code + "' id='" + _af_var_param + "[" + row
						+ "]." + field.code + "'/>" + "</span>");
		$("#recordarea").append("<span>" + field.remark + "</span>");
		$("#recordarea").append("</div>");
	}
	function init_field_double(row, field) {
		$("#recordarea").append("<div>");
		$("#recordarea").append("<span>" + field.name + "</span>");
		$("#recordarea").append(
				"<span>" + "<input name='" + _af_var_param + "[" + row + "]."
						+ field.code + "' id='" + _af_var_param + "[" + row
						+ "]." + field.code + "'/>" + "</span>");
		$("#recordarea").append("<span>" + field.remark + "</span>");
		$("#recordarea").append("</div>");
	}
	function fetch_template(name) {
		$.ajax( {
			url : "${ctx}/r/crt/" + name,
			type : "post",
			dataType : "json",
			timeout : 5000,
			error : function() {
				alert("Error loading XML document");
			},
			success : function(root) {
				var json = root.json;
				fetch_template_callback(json);
			}
		});
	}
	function fetch_template_callback(message) {
		if (typeof Object.toJSONString == "undefined") {
			$.getScript('${ctx}/static/js/json.js', function(js) {
				eval(js);
				customize_template(message);
			});
		}
	}
</script>
</body>
</html>

